package com.mzy.datepackage.M2022.July.eleventh;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mzy.basics.serializable.jsontoolclass.Person;
import com.mzy.datepackage.M2022.July.eleventh.common.SourceAddress;
import org.testng.annotations.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/7/11 15:20
 * @Version 1.0
 */
public class Replace {
    private final static String filePath = "F:\\每日脚本暂存\\新建文件路径\\2022.07.11\\DATA_RESOURCES_SOURCE_ADDRESS.json";

    /**
     * 全读取，按行匹配
     */
    @Test
    public void replace() {
//        List<String> read = read(filePath,StandardCharsets.UTF_8);
//        read.forEach(e -> System.out.println(e));
//        ArrayList<String> arrayListWrite = new ArrayList<>();
//        read.forEach(e -> {
//            if (e.contains("192.168.5.222")) {
//                // 替换成目标
//                String replace = e.replace(e, "\"ip\":"+"\"192.168.5.hhhhhhh\",");
//                arrayListWrite.add(replace);
//                return;
//            }
//            arrayListWrite.add(e);
//        });
//
//        arrayListWrite.forEach(e -> System.out.println(e));
//        write(arrayListWrite,filePath,false);

        String s = readFile(filePath);
        // 方式1: 将json字符串转为Java对象
        SourceAddress person = JSON.parseObject(s, SourceAddress.class);

        System.out.println("java对象-》" + person);



    }

    /**
     *
     * @param writeList
     * @param path
     * @param isAppend
     */
    public static void writeTest(List<SourceAddress> writeList, String path, boolean isAppend) {

        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(new File(path), isAppend), StandardCharsets.UTF_8))
        ) {
            String s = JSON.toJSONString(writeList);
            writer.write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 新增的时候根据传入路径写文件
     * 默认传入一条组装好的dns 映射规则
     * 追加的形式
     *
     * @param writeList
     * @param path
     */
    public static void write(List<String> writeList, String path, boolean isAppend) {
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(new File(path), isAppend), "UTF-8"))
        ) {
//            /** todo有待求证~
//             * windows \r\n
//             * linux \n
//             * mac \r
//             */
            for (String arr : writeList) {
                writer.write(arr);
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 传入文件路径
     * 按行读
     *
     * @param filePath
     * @return
     */
    public static List<String> read(String filePath,
                                    Charset encode) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encode))) {
            ArrayList<String> list = new ArrayList<>();
            // 按行读 放进Arraylist
            String tempString;
            while ((tempString = reader.readLine()) != null) {//BufferedReader有readLine()，可以实现按行读取
                list.add(tempString);
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 读取文件内容
     *
     * @param path
     * @return
     */
    public static String readFile(String path) {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf-8"))) {
            String line = null;
            StringBuilder builder = new StringBuilder();
            while ((line = in.readLine()) != null) {
                builder.append(line).append("\n");
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("??");
            return null;
        }
    }

    /**
     * 读String
     * 从文件中读取数据
     */
    public static String readStringFromFile(String path, String encode) {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(path), encode))) {
            String line = null;
            StringBuilder builder = new StringBuilder();
            while ((line = in.readLine()) != null) {
                builder.append(line).append("\n");
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
